package com.liyunc.demo.controller;

import com.liyunc.demo.model.entity.Department;
import com.liyunc.demo.model.entity.Role;
import com.liyunc.demo.model.entity.User;
import com.liyunc.demo.model.GeneralResponse;
import com.liyunc.demo.service.DepartmentService;
import com.liyunc.demo.service.RoleService;
import com.liyunc.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;


    @GetMapping("/test")
    public GeneralResponse test() {
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("测试通过");
        return response;
    }

    @PostMapping("/save")
    public GeneralResponse saveUser(@RequestParam String name, @RequestParam String deName,
                                    @RequestParam String roName) {
        Department department = new Department();
        department = departmentService.findDepartmentByName(deName);

        Role role = new Role();
        role = roleService.findRoleByName(roName);
        List<Role> list = new ArrayList<>();
        list.add(role);

        User user = new User();
        user.setUsername(name);
        user.setCreateDate(new Date());
        user.setDepartment(department);
        user.setRoles(list);

        log.info("save user");
        userService.saveUser(user);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("success");
        return response;
    }

    @DeleteMapping(value = "delete")
    public GeneralResponse deleteUser(@RequestParam Long id) {
        log.info("delete user");
        userService.deleteUserById(id);

        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("delete success");
        return response;
    }

    @PostMapping(value = "login")
    public GeneralResponse login(@Validated User user, BindingResult bindingResult) {
        log.info("user login");
        GeneralResponse response = new GeneralResponse();
        //1.JSR303
        if (bindingResult.hasErrors()) {
            response.setCode(400);
            response.setMessage(bindingResult.getFieldError().getDefaultMessage());
            return response;
        }
        //2.Shiro
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token =
            new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
            response.setCode(200);
            response.setMessage("login success");
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(400);
            response.setMessage("username or password is error");
            return response;
        }
    }


    @RequiresPermissions({"select"})
    @GetMapping("select")
    public GeneralResponse selectPermission() {
        log.info("拥有查看权限");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有查看权限");
        return response;
    }

    @RequiresPermissions({"insert"})
    @GetMapping("insert")
    public GeneralResponse insertPermission() {
        log.info("拥有增加权限");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有增加权限");
        return response;
    }

    @RequiresPermissions({"update"})
    @GetMapping("update")
    public GeneralResponse updatePermission() {
        log.info("拥有修改权限");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有修改权限");
        return response;
    }

    @RequiresPermissions({"delete"})
    @GetMapping("delete")
    public GeneralResponse deletePermission() {
        log.info("拥有删除权限");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有删除权限");
        return response;
    }

    @RequiresRoles({"vip"})
    @GetMapping("vip")
    public GeneralResponse vipRole() {
        log.info("拥有vip角色");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有vip角色");
        return response;
    }

    @RequiresRoles({"ip"})
    @GetMapping("ip")
    public GeneralResponse ipRole() {
        log.info("拥有ip角色");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有ip角色");
        return response;
    }

    @RequiresRoles({"p"})
    @GetMapping("p")
    public GeneralResponse pRole() {
        log.info("拥有p角色");
        GeneralResponse response = new GeneralResponse();
        response.setCode(200);
        response.setMessage("拥有p角色");
        return response;
    }

}
